package com.blue.controller.system;

import com.blue.common.annotation.ReplaceParam;
import com.blue.common.entity.Relation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.system.ErrorLogsPO;
import com.blue.dto.system.ErrorLogsDTO;
import com.blue.service.system.IErrorLogsService;
import com.blue.common.entity.ResponseResult;

import java.util.List;


/**
 * 系统错误日志表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2024-12-07
 */
@RestController
@RequestMapping("/system/errorLogs")
@Tag(name = "系统错误日志表")
public class ErrorLogsController {
    @Autowired
    private IErrorLogsService errorLogsService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("requestUrl")
    public ResponseResult<?> list(ErrorLogsDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        String operatorNm = requestDTO.getOperatorNm();
        String startTime = requestDTO.getStartTime();
        String endTime = requestDTO.getEndTime();
        ErrorLogsPO errorLogsPO = ConvertUtils.convert(ErrorLogsPO.class, requestDTO);
        Relation relation = new Relation("username", "author_user t1 on t.operator = t1.user_id");
        MPJQueryWrapper<ErrorLogsPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(errorLogsPO, relation);
        queryWrapper.ge(StringUtils.isNotBlank(startTime), "t.create_time", startTime);
        queryWrapper.le(StringUtils.isNotBlank(endTime), "t.create_time", endTime);
        queryWrapper.like(StringUtils.isNotBlank(operatorNm), "t1.username", operatorNm);
        Page<ErrorLogsPO> page = new Page<>(pageNum, pageSize);
        Page<ErrorLogsPO> pageResult = errorLogsService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody ErrorLogsDTO requestDTO) {
        ErrorLogsPO errorLogsPO = ConvertUtils.convert(ErrorLogsPO.class, requestDTO);
        errorLogsService.save(errorLogsPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody ErrorLogsDTO requestDTO) {
        ErrorLogsPO errorLogsPO = ConvertUtils.convert(ErrorLogsPO.class, requestDTO);
        errorLogsService.updateById(errorLogsPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        errorLogsService.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody ErrorLogsDTO requestDTO) {
        ErrorLogsPO errorLogsPO = errorLogsService.getById(requestDTO.getId());
        return ResponseResult.okResult(errorLogsPO);
    }
}
